package cn.tedu.shoot;

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class World extends JPanel{
    public static final int WIDTH = 400;
    public static final int HEIGHT = 700;
    public static final int RUNNING = 1;
    public static final int PAUSE = 2;
    public static final int GAME_OVER = 3;
    public static final int START = 0;
    private int state = START;

    private Hero hero = new Hero();
    private Sky sky = new Sky();
    private FlyingObject[] airplanes= {};

    private Bullet[] bullet = {};

    // 生成敌人(小敌机、大敌机、小蜜蜂)对象
    public FlyingObject nextOne(){
        Random rand = new Random(); //随机数对象
        int type = rand.nextInt(20); //0到19之间的随机数
        if(type<5){ //0到4时，返回小蜜蜂对象
            return new Bee();
        }else if(type<13){ //5到12时，返回小敌机对象
            return new Airplane();
        }else{ //13到19时，返回大敌机对象
            return new BigAirplane();
        }
    }


    private int shootIndex = 0; //子弹入场计数
    // 子弹入场
    public void shootAction(){ //每10毫秒走一次
        shootIndex++; //每10毫秒增1
        if(shootIndex%30==0){ //每300(30*10)毫秒走一次
            Bullet[] bs = hero.shoot(); //获取子弹数组对象
            bullet = Arrays.copyOf(bullet,bullet.length+bs.length); //扩容(bs有几个就扩大几个容量)
            System.arraycopy(bs,0,bullet,bullet.length-bs.length,bs.length); //数组的追加
        }
    }

    private int enterIndex = 0; //敌人入场计数
    // 敌人入场
    public void enterAction(){ //每10毫秒走一次
        enterIndex++; //每10毫秒增1
        if(enterIndex%40==0){ //每400(40*10)毫秒走一次
            FlyingObject obj = nextOne(); //获取敌人对象
            airplanes = Arrays.copyOf(airplanes,airplanes.length+1); //扩容
            airplanes[airplanes.length-1] = obj; //将obj添加到enemies的最后一个元素上
        }
    }

    //天空对象移动
    private void moveAction(){
        sky.step();
        for(int i = 0;i<airplanes.length;i++){
            airplanes[i].step();
        }
        for(int i = 0;i<bullet.length;i++){
            bullet[i].step();
        }
    }

    //删除越界对象
    private void outOfBounds(){
        for(int i = 0;i<airplanes.length;i++){
            if(airplanes[i].isOutOfBounds() || airplanes[i].isRemove()){
                airplanes[i] = airplanes[airplanes.length-1];
                airplanes = Arrays.copyOf(airplanes,airplanes.length-1);
            }
        }
        for(int i = 0;i<bullet.length;i++){
            if(bullet[i].isOutOfBounds() || bullet[i].isRemove()){
                bullet[i] = bullet[bullet.length-1];
                bullet = Arrays.copyOf(bullet,bullet.length-1);
            }
        }
    }

    //子弹与敌人碰撞
    private int score = 0;
    public void bulletBandAction(){
        for(int i = 0;i<bullet.length;i++){
            Bullet b =bullet[i];
            for(int j = 0;j<airplanes.length;j++){
                FlyingObject f = airplanes[j];
                if(b.isLive() && f.isLive() && f.isHit(b)){
                    b.goDead();
                    f.goDead();
                    if(f instanceof EnemyScore){
                        EnemyScore e = (EnemyScore) f;
                        score +=e.getScore();
                    }
                    if(f instanceof EnemyAward){
                        EnemyAward es = (EnemyAward) f;
                        int type = es.getAwardType();
                        switch (type){
                            case EnemyAward.FIRE:
                                hero.addFire();
                                break;
                            case EnemyAward.LIFE:
                                hero.addLife();
                                break;
                        }
                    }
                }
            }
        }
    }

    //检测英雄机和敌人碰撞,英雄机减命。敌人死
    private void heroBangAction(){
        for(int i = 0;i<airplanes.length;i++){
            FlyingObject f = airplanes[i];
            if(f.isLive() && hero.isLive() && f.isHit(hero)){
                f.goDead();
                hero.subtractLife();
                hero.clearFire();
            }
        }
    }

    //检测游戏结束
    private void checkGameOverAction(){
        if(hero.getLife()<=0){
            state = GAME_OVER;
        }
    }


    // 启动程序的执行
    public void action(){
        MouseAdapter m = new MouseAdapter() {
            //重写鼠标移动事件
            @Override
            public void mouseMoved(MouseEvent e) {
                if(state == RUNNING){
                    int x = e.getX();
                    int y = e.getY();
                    hero.moveTo(x,y);
                }
            }
            //重新鼠标点击事件
            @Override
            public void mouseClicked(MouseEvent e) {
                switch (state){
                    case START:
                        state = RUNNING;
                        break;
                    case GAME_OVER:
                        score = 0;
                        sky = new Sky();
                        hero = new Hero();
                        airplanes = new FlyingObject[0];
                        bullet = new Bullet[0];
                        state = START;
                        break;
                }
            }
            //重写鼠标移出事件
            @Override
            public void mouseExited(MouseEvent e){
                if(state == RUNNING){
                    state = PAUSE;
                }
            }
            //重写鼠标移入事件
            @Override
            public void mouseEntered(MouseEvent e){
                if(state == PAUSE){
                    state = RUNNING;
                }
            }
        };
        this.addMouseListener(m);
        this.addMouseMotionListener(m);

        Timer timer = new Timer(); //定时器对象
        int interval = 10; //定时间隔(以毫秒为单位)
        timer.schedule(new TimerTask(){
            public void run(){ //定时干的事(每10毫秒走一次)
                if(state == RUNNING){
                    enterAction(); //敌人入场
                    shootAction(); // 子弹入场
                    moveAction();
                    bulletBandAction();
                    heroBangAction();
                    checkGameOverAction();
                }
                repaint(); //重画(重新调用paint()方法)
            }
        },interval,interval); //定时计划表
    }

    public void paint(Graphics g){
        g.drawImage(sky.getImage(), sky.x, sky.y, null);
        g.drawImage(sky.getImage(),sky.x,sky.getY1(),null); //画天空2
        g.drawImage(hero.getImage(),hero.x,hero.y,null); //画英雄机
        for(int i=0;i<airplanes.length;i++){ //遍历所有敌人
            FlyingObject f = airplanes[i]; //获取每一个敌人
            g.drawImage(f.getImage(),f.x,f.y,null); //画敌人
        }
        for(int i=0;i<bullet.length;i++){ //遍历所有子弹
            Bullet b = bullet[i]; //获取每一个子弹
            g.drawImage(b.getImage(),b.x,b.y,null); //画子弹
        }
        g.drawString("SCORE: "+score,10,25); //画分
        g.drawString("LIFE: "+hero.getLife(),10,45); //画命
        //根据当前状态来画不同的状态图
        switch (state){
            case START:
                g.drawImage(Images.start,0,0,null);
                break;
            case PAUSE:
                g.drawImage(Images.pause,0,0,null);
                break;
            case GAME_OVER:
                g.drawImage(Images.gameover,0,0,null);
                break;
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        World world = new World();
        world.setFocusable(true);
        frame.add(world);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(World.WIDTH+16,World.HEIGHT+39);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        world.action();
    }
}
